package vn.vfriends.payment.jpa.entity;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import org.eclipse.persistence.annotations.ConversionValue;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.ObjectTypeConverter;

/**
 *
 * @author tuan@vfriends.vn
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Transaction.findByType", query = "SELECT t FROM Transaction t WHERE t.account = :account AND t.type = :type")})
public class Transaction implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    private Long id;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date transactionDate;
    
    @ManyToOne
    @JoinColumn(name = "ACCOUNT_ID")
    private Account account;
    
    private Long amount;

    public enum Type {

        IN("IN"), OUT("OUT");
        private String type;

        private Type(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        @Override
        public String toString() {
            return type;
        }
    }
    @ObjectTypeConverter(name = "transactionTypeConverter", objectType = Type.class, dataType = String.class, conversionValues = {
        @ConversionValue(objectValue = "IN", dataValue = "IN"),
        @ConversionValue(objectValue = "OUT", dataValue = "OUT")})
    @Convert("transactionTypeConverter")
    @Column(length = 10)
    private Type type;
    
    @Column(length=100)
    private String source;
    
    @Column(length=200)
    private String description;

    @PrePersist
    public void initTimeStamps() {
        if (transactionDate == null) {
            transactionDate = new Date();
        }
    }
    
    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public Long getAmount() {
        return amount;
    }

    public void setAmount(Long amount) {
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public Date getTransactionDate() {
        return transactionDate;
    }

    public void setTransactionDate(Date transactionDate) {
        this.transactionDate = transactionDate;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }
}
